package com.xinxin.leetcode.problem690;

import java.util.*;

/**
 * @author ShiXinXin
 * @date 2020-02-24 11:46
 */
class Solution {
    private Integer sum = 0;

    private void setEmployee(Map<Integer, Employee> map, int id) {
        sum += map.get(id).importance;
        for (Integer subId : map.get(id).subordinates) {
            setEmployee(map, subId);
        }
    }

    public int getImportance(List<Employee> employees, int id) {
        Map<Integer, Employee> map = new HashMap<>();
        for (Employee employee : employees) {
            map.put(employee.id, employee);
        }
        setEmployee(map, id);
        return sum;
    }

    public static void main(String[] args) {
        List<Integer> subordinates = new ArrayList<>();
        Employee employee1 = new Employee(1, 5, new ArrayList<Integer>() {{
            add(2);
            add(3);
        }});
        Employee employee2 = new Employee(2, 3, new ArrayList<Integer>() {{
        }});
        Employee employee3 = new Employee(3, 3, new ArrayList<Integer>() {{
        }});
        List<Employee> employees = new ArrayList<Employee>() {{
            add(employee1);
            add(employee2);
            add(employee3);
        }};
        System.out.println(new Solution().getImportance(employees, 1));
    }
}
